exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
  constructor() {
    super();
    this.callExtenMode = this.db["call_exten"];
    this.userModel = this.db['sys_user'];
    this.callExtenMode.belongsTo(this.userModel, {
      foreignKey: 'commissioner',
      targetKey: 'id'
    });
  }
  /**
   * 更新、编辑分机数据
   *
   * @param {Object} data   分机基础数据
   * @returns
   */
  doAddOrUpdate(data) {
    var _this = this;

    return _asyncToGenerator(function* () {
      const db = _this.db;
      try {
        const oldExten = yield _this.callExtenMode.findOne({
          where: {
            exten: data.exten
          }
        });
        if (data.id) {
          return yield db["call_exten"].update(data, {
            where: {
              id: data.id
            }
          });
        } else {
          if (oldExten) return false;
          return yield db["call_exten"].create(data);
        }
      } catch (err) {
        return _this.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 获取分机数据 分页
   *
   * @param {any} data
   * @returns
   */
  doGetExtenListForPage(data) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const db = _this2.db;
      data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
      let where = {};
      if (data.exten) where.exten = {
        [_this2.db.Sequelize.Op.like]: "%" + data.exten + "%"
      };
      try {
        return yield db["call_exten"].findAndCountAll({
          where: where,
          order: [["id", "DESC"]],
          include: [{
            model: _this2.userModel,
            attributes: ['id', 'realname']
          }],
          limit: parseInt(data.pageSize),
          offset: parseInt(data.start)
        });
      } catch (err) {
        return _this2.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 通过id查询分机数据
   *
   * @param {any} data
   * @returns
   */
  doGetExtenListById(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const db = _this3.db;
      try {
        return yield db["call_exten"].findOne({
          where: {
            id: data.id
          }
        });
      } catch (error) {
        return _this3.exceptionService.handleError(err);
      }
    })();
  }

  doDelExtenById(data) {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const db = _this4.db;
      try {
        return yield db["call_exten"].destroy({
          where: {
            id: data.id
          }
        });
      } catch (error) {
        return _this4.exceptionService.handleError(err);
      }
    })();
  }
};